<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <PageTools>
            <template #left>
              <span>总记录数：{{ total }}条</span>
            </template>
            <template #right>
              <el-button
                type="warning"
                size="small"
                @click="$router.push('/import')"
              >
                导入excel
              </el-button>
              <el-button type="danger" size="small">导出excel</el-button>
              <el-button type="primary" size="small" @click="showDialog = true">
                新增员工
              </el-button>
            </template>
          </PageTools>
        </div>
        <div>
          <!-- table列表 -->
          <!-- :default-sort 可以指定当前页加载后，数据按照那一列进行排序 -->
          <!-- prop指定排序列 -->
          <!-- order 排序方式：ascending 升序 descending -->
          <el-table
            border
            :data="list"
            :default-sort="{ prop: 'workNumber', order: 'ascending' }"
          >
            <el-table-column type="index" label="序号" />
            <el-table-column prop="username" label="姓名" />
            <el-table-column prop="workNumber" label="工号" />
            <el-table-column prop="formOfEmployment" label="聘用形式">
              <template #default="{ row }">
                <p>{{ formatEmployment(row.formOfEmployment) }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="departmentName" label="部门" />
            <el-table-column sortable prop="correctionTime" label="入职时间" />
            <el-table-column label="账户状态">
              <el-switch v-model="active"></el-switch>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="280">
              <template #default="{ row }">
                <el-button type="text" size="small">查看</el-button>
                <el-button type="text" size="small">分配角色</el-button>
                <el-button
                  type="text"
                  size="small"
                  @click="delEmployee(row.id)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row
            type="flex"
            justify="center"
            align="middle"
            style="height: 60px"
          >
            <el-pagination
              :current-page="params.page"
              :page-size="params.size"
              :total="total"
              :page-sizes="[2, 5, 10]"
              layout="prev, pager, next, sizes"
              @current-change="changePage"
              @size-change="changeSize"
            />
          </el-row>
        </div>
      </el-card>
    </div>
    <!-- 新增员工 -->
    <AddEmployee :show-dialog="showDialog" @close-dialog="showDialog = false" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
// 导入数据字典，枚举类型
import dataTypes from '@/api/constant/employees'
import AddEmployee from './components/add-employee.vue'
console.log(dataTypes)
// 使用作用域插槽获取当前行数据
// 在methods中定义格式化方法
export default {
  components: {
    AddEmployee
  },
  data () {
    return {
      showDialog: false,
      active: true,
      list: [],
      params: {
        page: 1, // 当前页
        size: 10 // 每页条数
      },
      total: 0 // 总数
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 删除员工 当前点击要删员工的id
    delEmployee (id) {
      // 用户确认
      // 调用接口
      // 刷新列表
      this.$confirm('确认删除么？', '提示').then(async () => {
        // 点击确定会执行
        await delEmployee(id)
        this.$message.success('删除成功！')

        // 纠正页面错误 已经删除一条数据了，所以要减一
        const newPage = Math.ceil((this.total - 1) / this.params.size)
        // 判断 页面大于0，最新是1
        if (newPage > 0) {
          // 纠正页码
          this.params.page = this.params.page > newPage ? newPage : this.params.page
        }
        this.getList()
      }).catch(console.log)
    },
    // 格式化聘用形式 通过数据字典，根据数字找到数字代表的含义
    formatEmployment (type) {
      // 生成映射关系，根据映射关系找对应数字的value值
      const maps = {}
      dataTypes.hireType.forEach(item => {
        maps[item.id] = item.value
      })
      return maps[type]
    },
    // 切换每页多少数据
    changeSize (size) {
      this.params.size = size
      this.getList()
    },
    // 分页切换列表数据
    changePage (newPage) {
      this.params.page = newPage
      this.getList()
    },
    // 获取员工列表
    async getList () {
      const { total, rows } = await getEmployeeList(this.params)
      // console.log(total, rows)
      this.total = total
      this.list = rows
    }
  }
}
</script>

<style lang="less" scoped>
</style>
